package com.yx.cloud.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iot.common.utils.LogUtils;
import com.iot.sweeper.AppContext;
import com.yx.cloud.BuildConfig;
import com.yx.cloud.bean.RobotIconColorBean;
import com.yx.cloud.constant.Constants;
import com.iot.product.sweeper.bean.Robot;

import java.util.ArrayList;
import java.util.List;

/**
 * INXNI 机器图片颜色切换保存list
 */
public class SharedPreferencesList {
    private final static String TAG = "SharedPreferencesList";

    public static void switchRobotPicture(Context context, String isSwitch, String switch_icon, List<Robot> robotList, String tag) {

        List list = new ArrayList();
        Gson gson = new Gson();
        RobotIconColorBean bean;
        List<RobotIconColorBean> mList;
        //先取数据
        SharedPreferences sp = context.getSharedPreferences("SP_ROBOT_ICON", context.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出
        if ("add".equals(isSwitch)) {
            String peopleListJson = sp.getString("KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                //已经有了
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                for (int i = 0; i < robotList.size(); i++) {
                    String deviceId = robotList.get(i).getDeviceId();
                    if (robotList.get(i).getEquipmentType() == 0) {
                        int flag = -1;
                        for (int j = 0; j < mList.size(); j++) {
                            String deviceId1 = mList.get(j).getDeviceId();
                            if (deviceId.equals(deviceId1)) {
                                flag = j;
                                break;
                            }
                        }
                        if (flag == -1) {
                            bean = new RobotIconColorBean();
                            bean.setIsICutoverColor(switch_icon);
                            bean.setDeviceId(robotList.get(i).getDeviceId());
                            list.add(bean);
                        } else {
                            list.add(mList.get(flag));
                        }
                    }
                }
               // LogUtils.i(mList.size() + " : onFling aaaaaaaaaaaaaaaaaaaaaaa********** : " + robotList.size());

            } else {
                LogUtils.i(" : onFling jjjjjjjjjjjjjjjjjjjjjjjjjj********** : ");
                for (int i = 0; i < robotList.size(); i++) {
                    bean = new RobotIconColorBean();
                    int equipmentType = robotList.get(i).getEquipmentType();
                    if (equipmentType == 0) {
                        bean.setIsICutoverColor(switch_icon);
                        bean.setDeviceId(robotList.get(i).getDeviceId());
                        list.add(bean);
                    }
                }
            }
        } else if ("switch".equals(isSwitch)) {
            String peopleListJson = sp.getString("KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                //防空判断
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                if (mList.size() > 0) {
                    String connect_deviceId = AppContext.get().getCurrentDevice().getDeviceId();
                    for (int i = 0; i < mList.size(); i++) {
//                        Log.d(TAG, mList.get(i).getDeviceId() + " : onFling switch 000 ********** : " + Constants.CURRENTITEM);
                        if (mList.get(i).getDeviceId().equals(connect_deviceId)) {
                            LogUtils.i(" : onFling switch 111 ********** : " + switch_icon);
                            mList.get(i).setIsICutoverColor(switch_icon);//只修改颜色
                        }
                    }
                    list.addAll(mList);
                }
            }
        } else if ("delete".equals(isSwitch)) {
            LogUtils.i(" :  delete onFling jjjjjjjjjjjjjjjjjjjjjjjjjj********** : ");
            String peopleListJson = sp.getString("KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).getDeviceId().equals(Constants.DELETE_DEVICE_ID)) {
                        mList.remove(i);
                    }
                }
                list.addAll(mList);
            }
        }
        //保存上次选中的机器 颜色
        String jsonStr = gson.toJson(list); //将List转换成Json
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("KEY_RobotIcon_LIST_DATA", jsonStr); //存入json串
        editor.commit();  //提交
        LogUtils.i(isSwitch + " : onFling fffffffff********** : " + new Gson().toJson(list));
    }

    public static void switchRobotPictureX5(Context context, String isSwitch, String switch_icon, List<String> robotList, String switchDeviceId) {
        List list = new ArrayList();
        Gson gson = new Gson();
        RobotIconColorBean bean;
        List<RobotIconColorBean> mList;
        //先取数据
        SharedPreferences sp = context.getSharedPreferences("X5_SP_ROBOT_ICON", context.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出
        if ("add".equals(isSwitch)) {
            String peopleListJson = sp.getString("X5_KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                //已经有了
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                for (int i = 0; i < robotList.size(); i++) {
                    String deviceId = robotList.get(i);
                    int flag = -1;
                    for (int j = 0; j < mList.size(); j++) {
                        String deviceId1 = mList.get(j).getDeviceId();
                        if (deviceId.equals(deviceId1)) {
                            flag = j;
                            break;
                        }
                    }
                    if (flag == -1) {
                        bean = new RobotIconColorBean();
                        bean.setIsICutoverColor(switch_icon);
                        bean.setDeviceId(robotList.get(i));
                        list.add(bean);
                    } else {
                        list.add(mList.get(flag));
                    }
                }
                LogUtils.i(mList.size() + " x5 : onFling aaaaaaaaaaaaaaaaaaaaaaa********** : " + robotList.size());

            } else {
                LogUtils.i(" x5 : onFling jjjjjjjjjjjjjjjjjjjjjjjjjj********** : ");
                for (int i = 0; i < robotList.size(); i++) {
                    bean = new RobotIconColorBean();
                    bean.setIsICutoverColor(switch_icon);
                    bean.setDeviceId(robotList.get(i));
                    list.add(bean);
                }
            }
        } else if ("switch".equals(isSwitch)) {
            String peopleListJson = sp.getString("X5_KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                //防空判断
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                if (mList.size() > 0) {
                    for (int i = 0; i < mList.size(); i++) {
//                        Log.d(TAG, mList.get(i).getDeviceId() + " : onFling switch 000 ********** : " + Constants.CURRENTITEM);
                        if (mList.get(i).getDeviceId().equals(switchDeviceId)) {
                            LogUtils.i("x5 : onFling switch 111 ********** : " + switch_icon);
                            mList.get(i).setIsICutoverColor(switch_icon);//只修改颜色
                        }
                    }
                    list.addAll(mList);
                }
            }
        } else if ("delete".equals(isSwitch)) {
            LogUtils.i(" x5 :  delete onFling jjjjjjjjjjjjjjjjjjjjjjjjjj********** : ");
            String peopleListJson = sp.getString("X5_KEY_RobotIcon_LIST_DATA", "");  //取出key为"KEY_PEOPLE_DATA"的值，如果值为空，则将第二个参数作为默认值赋值
            if (peopleListJson != "") {
                mList = gson.fromJson(peopleListJson, new TypeToken<List<RobotIconColorBean>>() {
                }.getType());//将json字符串转换成List集合
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).getDeviceId().equals(switchDeviceId)) {
                        mList.remove(i);
                    }
                }
                list.addAll(mList);
            }
        }
        //保存上次选中的机器 颜色
        String jsonStr = gson.toJson(list); //将List转换成Json
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("X5_KEY_RobotIcon_LIST_DATA", jsonStr); //存入json串
        editor.commit();  //提交
        LogUtils.i(isSwitch + " x5 : onFling fffffffff********** : " + new Gson().toJson(list));
    }

}
